﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Web.Course;
using Telerik.WebControls;


public partial class sysadmin_DL_Project_AddLearner :PowerCreator.LMS.Web.Base.WebBasePage
{
    TOLearner obj=new TOLearner();
    Course opCourse = new Course();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {

        CheckUser();
        if (!IsPostBack)
        {
            
            InitPage();
            //BinderUserGroup();
            BinderRegion();
            BinderSchool(int.Parse(ddl_Region.SelectedValue));
            BinderSchoolStage();
            BinderCatalog();
            BinderXueNian();
            BinderJob();
            BinderDuty();
            BinderXueLi();
            BinderSepcial();
    
            int LID = 0; 
            if (Request.QueryString["LID"] != null)
            {
                LID = int.Parse(Request.QueryString["LID"].ToString());
                ViewState["LID"] = LID.ToString();
                BinderForm(LID);
            }
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 4)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }

       

    }
    private int CheckPopedom()
    {
        int rt = 0;

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            rt = login.VOAdvancedUser.AUserGroup;
        }
        return rt;

    }
    //绑定用户组
    //private void BinderUserGroup()
    //{
    //    TOUserGroup objUserGroup = new TOUserGroup();
    //    string search = "";
    //    DataTable dt = objUserGroup.Query(search);

    //    ddl_UserGroup.DataSource = dt;
    //    ddl_UserGroup.DataTextField = "Name";
    //    ddl_UserGroup.DataValueField = "UGroup_Id";
    //    ddl_UserGroup.DataBind();

    //}

    //绑定区域
    private void BinderRegion()
    {
        TORegion obj = new TORegion();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_Region.DataSource = dt;
        ddl_Region.DataTextField = "Region";
        ddl_Region.DataValueField = "ID";
        ddl_Region.DataBind();

    }

    //绑定学校
    private void BinderSchool(int RegionID)
    {
        TOSchoolGroup obj = new TOSchoolGroup();
        string search = "1=1";
        if (RegionID.ToString() != "0")
        {
            VOTORegion voRegion = new VOTORegion();
            TORegion objRegion = new TORegion();
            voRegion = objRegion.Load(RegionID);
            search += " and SchoolGroupRegion='" + voRegion.Region + "'";
        }

        DataTable dt = obj.Query(search);
        
        ddl_School.DataSource = dt;
        ddl_School.DataTextField = "SchoolGroupName";
        ddl_School.DataValueField = "SchoolGroup_Id";
        ddl_School.DataBind();


    }


    //绑定学段
    private void BinderSchoolStage()
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_xueduan_Stage.DataSource = dt;
        ddl_xueduan_Stage.DataTextField = "SchoolStage";
        ddl_xueduan_Stage.DataValueField = "ID";
        ddl_xueduan_Stage.DataBind();

    }

    //绑定学科
    private void BinderCatalog()
    {
        Catalog obj = new Catalog();
        string search = "";
        DataTable dt = obj.QueryCatalog(search);

        ddl_xeke_Catalog.DataSource = dt;
        ddl_xeke_Catalog.DataTextField = "CatalogName";
        ddl_xeke_Catalog.DataValueField = "CatalogId";
        ddl_xeke_Catalog.DataBind();

    }

    //绑定学年
    private void BinderXueNian()
    {
        ListItem li1 = new ListItem();
        li1.Text = "小学低年级";
        li1.Value = "1";
        ddl_xuenian.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "小学高年级";
        li2.Value = "2";
        ddl_xuenian.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "初一年级";
        li3.Value = "3";
        ddl_xuenian.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "初二年级";
        li4.Value = "4";
        ddl_xuenian.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "初三年级";
        li5.Value = "5";
        ddl_xuenian.Items.Add(li5);

        ListItem li6 = new ListItem();
        li6.Text = "高一年级";
        li6.Value = "6";
        ddl_xuenian.Items.Add(li6);
        ListItem li7 = new ListItem();
        li7.Text = "高二年级";
        li7.Value = "7";
        ddl_xuenian.Items.Add(li7);
        ListItem li8 = new ListItem();
        li8.Text = "高三年级";
        li8.Value = "8";
        ddl_xuenian.Items.Add(li8);
    }
    //绑定职务
    private void BinderJob()
    {
        Job obj = new Job();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_zhiwu.DataSource = dt;
        ddl_zhiwu.DataTextField = "TeajobName";
        ddl_zhiwu.DataValueField = "TeajobId";
        ddl_zhiwu.DataBind();

    }
    //绑定职称
    private void BinderDuty()
    {
        Duty obj = new Duty();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_zhicheng.DataSource = dt;
        ddl_zhicheng.DataTextField = "TeadutyName";
        ddl_zhicheng.DataValueField = "TeadutyId";
        ddl_zhicheng.DataBind();

    }
    //绑定当前学历
    private void BinderXueLi()
    {
        ListItem li1 = new ListItem();
        li1.Text = "中专";
        li1.Value = "1";
        ddl_xueli.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "大专";
        li2.Value = "2";
        ddl_xueli.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "本科";
        li3.Value = "3";
        ddl_xueli.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "硕士";
        li4.Value = "4";
        ddl_xueli.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "博士";
        li5.Value = "5";
        ddl_xueli.Items.Add(li5);

        ListItem li6 = new ListItem();
        li6.Text = "高中及以下";
        li6.Value = "6";
        ddl_xueli.Items.Add(li6);


    }
    //绑定特殊分类
    private void BinderSepcial()
    {
        ListItem li1 = new ListItem();
        li1.Text = "班主任";
        li1.Value = "1";
        ddl_Sepcial.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "外语教师";
        li2.Value = "2";
        ddl_Sepcial.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "新上岗教师";
        li3.Value = "3";
        ddl_Sepcial.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "市骨干";
        li4.Value = "4";
        ddl_Sepcial.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "一般教师";
        li5.Value = "5";
        ddl_Sepcial.Items.Add(li5);

        ListItem li6 = new ListItem();
        li6.Text = "特级教师";
        li6.Value = "6";
        ddl_Sepcial.Items.Add(li6);


    }
    private void BinderForm(int id)
    {
        VOTOLearner vo = new VOTOLearner();
        vo = obj.Load(id);

        txt_UserName.Text = vo.UserName;
        txt_UserName.Enabled = false;
        txt_UserPwd.Visible = false;
        txt_DBUserPwd.Visible = false;
        tr_dbpwd.Visible = false;
        tr_pwd.Visible = false;
        lbl_AddStudent_msg_username.Visible = false;
        //txt_UserQuestion.Text = vo.UserQuestion;
        //txt_UserAnswer.Text = vo.UserAnswer;
        //ddl_UserGroup.SelectedValue = vo.UserGroup.ToString();
        txt_TeacherId.Text = vo.TeacherId.ToString();
        txt_RealName.Text = vo.Name;
        txt_Email.Text = vo.Email;
        txt_Tel.Text = vo.Mobile;
        if (vo.Sex)
        {
            radbtn_Male.Checked= true;
            radbtn_Female.Checked = false;
        }
        else
        {
            radbtn_Male.Checked = false;
            radbtn_Female.Checked = true;
        }
        txt_Birth.Text = vo.Birthday.ToString();
        img_UserPic.ImageUrl = "../../../"+vo.UserPIC;
        ddl_Region.SelectedValue = vo.Region.ToString();
        ddl_School.SelectedValue = vo.School.ToString();
        ddl_Sepcial.SelectedValue = vo.Sepcial.ToString();
        ddl_xeke_Catalog.SelectedValue = vo.XueKe.ToString();
        ddl_xueduan_Stage.SelectedValue = vo.XueDuan.ToString();
        ddl_xueli.SelectedValue = vo.Education.ToString();
        ddl_xuenian.SelectedValue = vo.XueNian.ToString();
        ddl_zhicheng.SelectedValue = vo.ZhiCheng.ToString();
        ddl_zhiwu.SelectedValue = vo.ZhiWu.ToString();

    }

    protected override void InitPage()
    {
        lbl_InfoTitle_AddTeacher.Text = GetLanageValueByKey("lbl_AddTeacher_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Account_loading.Text = GetLanageValueByKey("lbl_AddTeacher_Account_loading", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_LoadingName.Text = GetLanageValueByKey("lbl_AddTeacher_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_PassWord.Text = GetLanageValueByKey("lbl_AddTeacher_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lblUserLoginIP.Text = GetLanageValueByKey("lbl_AddStudent_IsLoginIP", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_AddTeacher_BaseInformaition.Text = GetLanageValueByKey("lbl_AddTeacher_BaseInformaition", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Name.Text = GetLanageValueByKey("lbl_AddTeacher_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Number.Text = GetLanageValueByKey("lbl_AddTeacher_Number", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Sex.Text = GetLanageValueByKey("lbl_AddTeacher_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_IdNumber.Text = GetLanageValueByKey("lbl_AddTeacher_IdNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Born.Text = GetLanageValueByKey("lbl_AddTeacher_Born", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Mail.Text = GetLanageValueByKey("lbl_AddTeacher_Mail", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Class.Text = GetLanageValueByKey("lbl_AddTeacher_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Time.Text = GetLanageValueByKey("lbl_AddTeacher_Time", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Job.Text = GetLanageValueByKey("lbl_AddTeacher_Job", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Duty.Text = GetLanageValueByKey("lbl_AddTeacher_Duty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Phone.Text = GetLanageValueByKey("lbl_AddTeacher_Phone", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Native.Text = GetLanageValueByKey("lbl_AddTeacher_Native", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Post.Text = GetLanageValueByKey("lbl_AddTeacher_Post", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_MobileTelephone.Text = GetLanageValueByKey("lbl_AddTeacher_MobileTelephone", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Address.Text = GetLanageValueByKey("lbl_AddTeacher_Address", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Remark.Text = GetLanageValueByKey("lbl_AddTeacher_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_School.Text = GetLanageValueByKey("lbl_AddTeacher_School", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_XueLi.Text = GetLanageValueByKey("lbl_AddTeacher_XueLi", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Birth_css.Text = "(格式：1980-01-01)";
        //lab_JoinTime.Text = "(格式：1980-01-01)";
        radbtn_Male.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Female.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtUserName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtRealName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
        ftv_txtUserPwd.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btn_AddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_FileUpload_msg_FileType.Text = "只允许上传图片类型为:gif|jpg";
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private bool CheckPageData()
    {
        if (txt_Birth.Text.Trim().Length > 0)
        {
            try
            {
                DateTime times = DateTime.Parse(txt_Birth.Text.Trim());
            }
            catch (Exception ex)
            {
                lbl_Birth_err.Text = "出生日期格式不对，请检查！（例如：2000-1-12）";
                return false;
            }
        }
        try
        {
            int iPlans = int.Parse(ddl_School.SelectedValue);

        }
        catch (Exception ex)
        {
            lbl_School_err.Text = "<br />所属学校必须要选择，如果现在没有可以选择的，请先添加！";
            return false;
        }
        string search = string.Format("UserName = '{0}'", txt_UserName.Text);
        if (ViewState["LID"] != null)
        {
            search += "and Learner_Id<>" + ViewState["LID"].ToString();
        }
        if (obj.Count(search) != 0)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("AddTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
       
        if (!string.IsNullOrEmpty(txt_Email.Text))
        {
            if (!PageValidate.IsEmail(txt_Email.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Email.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Email.Focus();
                Script.Alert("请输入正确的邮箱地址！");
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
      
        if (!string.IsNullOrEmpty(txt_Tel.Text.Trim()))
        {
            if (!PageValidate.IsNumber(txt_Tel.Text))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g5", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Tel.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!PageValidate.IsNumber(txt_TeacherId.Text.Trim()))
        {
            Script.Alert("教师流水号只能输入数字来作为唯一标识！");
            return false;
        }

        if (txt_TeacherId.Text.Trim().Length>9)
        {
            Script.Alert("教师流水号最多只能输入9位！");
            return false;
        }
        return true;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOLearner vo = new VOTOLearner();

        if (ViewState["LID"] == null)
        {
            vo.UserName = txt_UserName.Text.Trim();
            vo.UserPwd = PowerCreator.LMS.Common.Utils.MD5Helper.GetMD5(txt_UserPwd.Text.Trim());
            //vo.UserQuestion = txt_UserQuestion.Text.Trim();
            //vo.UserAnswer = txt_UserAnswer.Text.Trim();
            //vo.UserGroup = int.Parse(ddl_UserGroup.SelectedValue);
            vo.TeacherId = int.Parse(txt_TeacherId.Text.Trim());


            vo.Name = txt_RealName.Text.Trim();
            vo.UserPIC = img_UserPic.ImageUrl;
            vo.Email = txt_Email.Text.Trim();
            vo.Mobile = txt_Tel.Text.Trim();
            if (radbtn_Female.Checked == true)
                vo.Sex = true;
            if (radbtn_Male.Checked == true)
                vo.Sex = false;
            if (txt_Birth.Text.Trim().Length > 0)
                vo.Birthday = DateTime.Parse(txt_Birth.Text.Trim());
            vo.Region = int.Parse(ddl_Region.SelectedValue);
            vo.School = int.Parse(ddl_School.SelectedValue);
            vo.XueDuan = int.Parse(ddl_xueduan_Stage.SelectedValue);
            vo.XueKe = int.Parse(ddl_xeke_Catalog.SelectedValue);
            vo.XueNian = int.Parse(ddl_xuenian.SelectedValue);
            vo.ZhiCheng = int.Parse(ddl_zhicheng.SelectedValue);
            vo.ZhiWu = int.Parse(ddl_zhiwu.SelectedValue);
            vo.Education = int.Parse(ddl_xueli.SelectedValue);
            vo.Sepcial = int.Parse(ddl_Sepcial.SelectedValue);

            vo.LoginNum = 0;

            if (obj.Add(vo))
            {
                string url = "ManageLearner.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = obj.Load(int.Parse(ViewState["LID"].ToString()));
            //vo.UserName = txt_UserName.Text.Trim();
            //vo.UserPwd = txt_UserPwd.Text.Trim();
            //vo.UserQuestion = txt_UserQuestion.Text.Trim();
            //vo.UserAnswer = txt_UserAnswer.Text.Trim();
            //vo.UserGroup = int.Parse(ddl_UserGroup.SelectedValue);
            vo.TeacherId = int.Parse(txt_TeacherId.Text.Trim());


            vo.Name = txt_RealName.Text.Trim();
            vo.UserPIC = img_UserPic.ImageUrl;
            vo.Email = txt_Email.Text.Trim();
            vo.Mobile = txt_Tel.Text.Trim();
            if (radbtn_Female.Checked == true)
                vo.Sex = false;
            if (radbtn_Male.Checked == true)
                vo.Sex = true;
            vo.Birthday = DateTime.Parse(txt_Birth.Text.Trim());
            vo.Region = int.Parse(ddl_Region.SelectedValue);
            vo.School = int.Parse(ddl_School.SelectedValue);
            vo.XueDuan = int.Parse(ddl_xueduan_Stage.SelectedValue);
            vo.XueKe = int.Parse(ddl_xeke_Catalog.SelectedValue);
            vo.XueNian = int.Parse(ddl_xuenian.SelectedValue);
            vo.ZhiCheng = int.Parse(ddl_zhicheng.SelectedValue);
            vo.ZhiWu = int.Parse(ddl_zhiwu.SelectedValue);
            vo.Education = int.Parse(ddl_xueli.SelectedValue);
            vo.Sepcial = int.Parse(ddl_Sepcial.SelectedValue);

            if (obj.Update(vo))
            {
                string url = "ManageLearner.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "ManageLearner.aspx";
        Script.RedirectURL(url);
    }




    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //btn_Back.Enabled = false;
        //Session["pwd"] = txt_UserPwd.Text;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Url = "";
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/UserPic/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);
            img_UserPic.ImageUrl = Url;
            //txt_UserPwd.Text = Session["pwd"].ToString();
            btn_Back.Enabled = true;
        }
    }

    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "gif|jpg";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            lbl_FileUpload_msg_FileType.Text ="上传文件格式不正确！"+lbl_FileUpload_msg_FileType.Text;
            return false;
        }
        return true;
    }

    protected void ddl_Region_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderSchool(int.Parse(ddl_Region.SelectedValue));
    }
}
